﻿/////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//Copyright (c) 2009 foldier inc.
//
//Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 
//associated documentation files (the "Software"), to deal in the Software without restriction, including 
//without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
//copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the 
//following conditions:
//
//The above copyright notice and this permission notice shall be included in all copies or substantial portions
//of the Software.
//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED 
//WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 
//PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 
//ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
//LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
//INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
//OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 
//IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////

using System;
using System.Data;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Net;
using System.Configuration;
using System.Collections.Generic;
using System.Text.RegularExpressions;

#if SILVERLIGHT
using System.Windows.Browser;
#else
using System.Web;
using System.Xml.XPath;
#endif

namespace foldier.API
{
    /// <summary>
    /// BaseApi derived class.
    /// Implements folder items API calls
    /// </summary>
    public class Folders : BaseApi
	{
		///////////////////////////////////////////////////////////////////////////////////////////////// 

		#region Protected local members

		protected long m_folderID = -1;

		#endregion

		///////////////////////////////////////////////////////////////////////////////////////////////// 

		#region Constructors
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="connector">FoldierOAuthConnector object for authentication</param>
		public Folders(FoldierOAuthConnector connector) : base(connector)
		{
		}
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="connector">FoldierOAuthConnector object for authentication</param>
        /// <param name="folderID">Folder ID</param>
		public Folders(FoldierOAuthConnector connector, long folderID)	: base(connector)
		{
			m_folderID = folderID;
		}


		#endregion

		///////////////////////////////////////////////////////////////////////////////////////////////// 

		#region API CALLS

		/// <summary>
        ///  Returns the single folder object.
		/// </summary>
		/// <returns>BaseItem object</returns>
		public BaseItem get() { return get(m_folderID); }
        
        /// <summary>
        /// Returns the single folder within the specified ID
        /// </summary>
        /// <param name="folderID">Folder ID</param>
        /// <returns>BaseItem object</returns>
        public BaseItem get(long folderID)
		{
			string result = string.Empty;
			if (folderID <= 0)
				return null;

			result = m_connection.genericApiCall(string.Format("{0:x}/folders/{1:x}?out=fullcontent", m_connection.UserID, folderID), string.Empty);

			return m_connection.resultItemFolder(result);

		}

        /// <summary>
        ///		Returns all the folders within the connected account.
        ///		Data returned in Alphabetical order.
        /// </summary>
        /// <returns>ItemFolder array</returns>
        public ItemFolder[] getAll() { return getAll(0, 20); }
        
        /// <summary>
        ///		Returns all the folders within the connected account.
        ///		Data returned in Alphabetical order.
        /// </summary>
        /// <param name="start">Index of first element to return</param>
        /// <param name="count">Number of elements to return</param>
        /// <returns>ItemFolder array</returns>
        public ItemFolder[] getAll(int start, int count)
        {
            string result = m_connection.genericApiCall(string.Format("{0:x}/folders?start={1}&count={2}&format=xml&sort=AlphabeticalAToZ&out=full", m_connection.UserID, start, count)
                                                       , string.Empty);

            return m_connection.resultItemFolders(result);
        }

        /// <summary>
        ///     Retrieves the ItemLinks array of a folder.
        ///     Data returned in Alphabetical order
        /// </summary>
        /// <returns>ItemLink array</returns>
        public ItemLink[] getLinks() { return getLinks(m_folderID, 0, 20); }
        
        /// <summary>
        ///     Retrieves the ItemLinks array of a specific folder.
        ///     Data returned in Alphabetical order
        /// </summary>
        /// <param name="folderID">Folder ID</param>
        /// <returns>ItemLink array</returns>
        public ItemLink[] getLinks(long folderID) { return getLinks(m_folderID, 0, 20); }
        
        /// <summary>
        ///     Retrieves the ItemLinks array of a specific folder.
        ///     Data returned in Alphabetical order
        /// </summary>
        /// <param name="start">Index of first element to return</param>
        /// <param name="count">Number of elements to return</param>
        /// <returns>ItemLink array</returns>
        public ItemLink[] getLinks(int start, int count) { return getLinks(m_folderID, start, count); }
        
        /// <summary>
        ///     Retrieves the ItemLinks array of a specific folder.
        ///     Data returned in Alphabetical order
        /// </summary>
        /// <param name="folderID">Folder ID</param>
        /// <param name="start">Index of first element to return</param>
        /// <param name="count">Number of elements to return</param>
        /// <returns>ItemLink array</returns>
        public ItemLink[] getLinks(long folderID, int start, int count)
        {
            string result = string.Empty;
            if (folderID > 0)
                result = m_connection.genericApiCall(string.Format("{0:x}/folders/{1:x}?start={2}&count={3}&format=xml&sort=ModifiedNewToOld&out=fullcontent"
                                                    , m_connection.UserID, folderID, start, count), string.Empty);
            else
                result = m_connection.genericApiCall(string.Format("{0:x}/folders?start={2}&count={3}&format=xml&sort=ModifiedNewToOld&out=fullcontent"
                                                    , m_connection.UserID, start, count), string.Empty);

            return m_connection.resultItemLinks(result);
        }

        /// <summary>
        ///     Search for folders matching query expression
        /// </summary>
        /// <param name="query">Search query</param>
        /// <returns>ItemFolder array</returns>
        public ItemFolder[] search(string query) { return search(query, 0, 20); }
        
        /// <summary>
        /// Search for folders matching query expression
        /// </summary>
        /// <param name="query">Search query</param>
        /// <param name="start">Index of first element to return</param>
        /// <param name="count">Number of elements to return</param>
        /// <returns>ItemFolder array</returns>
        public ItemFolder[] search(string query, int start, int count)
        {
            string result = m_connection.genericApiCall(string.Format("{0:x}/folders?q={1}&start={2}&count={3}&format=xml&sort=ModifiedNewToOld&out=full", m_connection.UserID, query, start, count)
                                                       , string.Empty);

            return m_connection.resultItemFolders(result);

        }

        /// <summary>
        ///     Search for ItemLinks matching query expression
        /// </summary>
        /// <param name="query">Search query</param>
        /// <returns>ItemFolder array</returns>
        public ItemLink[] searchLinks(string query) { return searchLinks(m_folderID, query, 0, 20); }
        
        /// <summary>
        ///     Search for ItemLinks matching query expression
        /// </summary>        
        /// <param name="folderID"></param>
        /// <param name="query">Search query</param>
        /// <returns>ItemFolder array</returns>
        public ItemLink[] searchLinks(long folderID, string query) { return searchLinks(m_folderID, query, 0, 20); }
        
        /// <summary>
        ///     Search for ItemLinks matching query expression
        /// </summary>
        /// <param name="query">Search query</param>
        /// <param name="start">Index of first element to return</param>
        /// <param name="count">Number of elements to return</param>
        /// <returns>ItemFolder array</returns>
        public ItemLink[] searchLinks(string query, int start, int count) { return searchLinks(m_folderID, query, start, count); }
        
        /// <summary>
        ///     Search for ItemLinks matching query expression
        /// </summary>
        /// <param name="query">Search query</param>
        /// <param name="start">Index of first element to return</param>
        /// <param name="count">Number of elements to return</param>
        /// <returns>ItemFolder array</returns>
        public ItemLink[] searchLinks(long folderID, string query, int start, int count)
        {
            string result = string.Empty;
            if (folderID > 0)
                result = m_connection.genericApiCall(string.Format("{0:x}/folders/{1:x}?q={2}&start={3}&count={4}&format=xml&out=fullcontent&sort=ModifiedNewToOld"
                                                    , m_connection.UserID, folderID, FoldierOauth.UriEncode(query), start, count), string.Empty);
            else
                result = m_connection.genericApiCall(string.Format("{0:x}/folders?q={1}&start={2}&count={3}&format=xml&out=fullcontent&sort=ModifiedNewToOld"
                                                    , m_connection.UserID, FoldierOauth.UriEncode(query), start, count), string.Empty);

            return m_connection.resultItemLinks(result);
        }

		#endregion

		///////////////////////////////////////////////////////////////////////////////////////////////// 
	}
}
